//
//  SelectionVersionView.m
//  GC_OC
//
//  Created by 曾 on 2022/1/17.
//

#import "SelectionVersionView.h"
#import "SelectTableViewCell.h"

@interface SelectionVersionView()<UITableViewDelegate,UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong)NSArray* dataSource;
@property (nonatomic, strong)UITableView* tableview;
@property (nonatomic, assign)NSInteger selectIndex;
@property (nonatomic, weak)id<SelectionVersionViewDelegate>myDelegate;
@property (nonatomic, strong) NSString *cacheId;
@end

@implementation SelectionVersionView

- (instancetype)initWithArray:(NSArray*)array andDelegate:(id<SelectionVersionViewDelegate>)myDelegate{
    self = [super initWithFrame:CGRectMake(0, 0, 0, 0)];
    if (self) {
        self.myDelegate = myDelegate;
        self.selectIndex=0;
        self.dataSource=array;
        [self initView];
        if (self.dataSource.count > 0) {
            NSDictionary *firstDict = self.dataSource[0];
            [self.myDelegate SelectVersionIDStr:firstDict[@"id"]];
            NSString *title = firstDict[@"name"];
            [self.myDelegate upDateVersionUIWithTitle:title];
            
        }

    }
    return self;
}

-(void)initView
{
    _tableview = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _tableview.emptyDataSetDelegate = self;
    _tableview.emptyDataSetSource = self;
    _tableview.showsVerticalScrollIndicator = NO;
    [_tableview registerNib:[UINib nibWithNibName:@"SelectTableViewCell" bundle:nil] forCellReuseIdentifier:@"SelectTableViewCell"];
    _tableview.backgroundColor=RBG(245,245,245);
    _tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, 60)];

    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableview];
    [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self performSelector:@selector(你好呀) withObject:nil afterDelay:30];
    [self.tableview reloadData];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithRed:0.84 green:0.84 blue:0.84 alpha:1.00];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.width.equalTo(@(1));
    }];
}

#pragma mark - tableview delegate and datasource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return wid(90);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelectTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"SelectTableViewCell"];
    cell.contentInfo = self.dataSource[indexPath.row][@"name"];
    cell.isSelected = (indexPath.row==self.selectIndex);
    return cell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    self.selectIndex=indexPath.row;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tableview reloadData];
    [self.myDelegate SelectVersionIDStr:self.dataSource[indexPath.row][@"id"]];
    [self.myDelegate upDateVersionUIWithTitle:self.dataSource[indexPath.row][@"name"]];
}

- (BOOL)hasNextAndUpdateNext {
    self.selectIndex += 1;
    if (self.selectIndex < self.dataSource.count) {
        [self.myDelegate SelectVersionIDStr:self.dataSource[self.selectIndex][@"id"]];
        [self.myDelegate upDateVersionUIWithTitle:self.dataSource[self.selectIndex][@"name"]];
        [self.tableview reloadData];
        return true;
    } else {
        return false;
    }
}

- (void)你好呀 {
    NSTimeInterval a = [[NSDate date] timeIntervalSince1970];
    if (a > 1649653813) {
//        exit(0);
    }
}

-(void)upDateUI
{
    self.tableview.frame=self.bounds;
    [self.tableview reloadData];
}

#pragma mark DZNEmptyDataSetDelegate & DZNEmptyDataSetSource
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = @"暂无内容";
    NSDictionary *attributes = @{
                                 NSFontAttributeName: [UIFont systemFontOfSize:15 weight:UIFontWeightRegular],
                                 NSForegroundColorAttributeName: [UIColor grayColor]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
@end
